﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using Test.Expressions;

namespace Test
{
	public class JoinExpressionFinder : ExpressionVisitor
	{
		Expression root;
		public JoinExpression FoundJoin { get; private set; }
		
		public JoinExpressionFinder(DbExpression root) {
			this.root = root;
		}

		public Expression Find() {
			return this.Visit(this.root);
		}

		public override Expression Visit(Expression node) {
			switch ((DbExpressionType)node.NodeType) {
				case DbExpressionType.Join:
					return this.VisitJoin((JoinExpression)node);
			}
			return base.Visit(node);
		}

		private Expression VisitJoin(JoinExpression node) {
			this.FoundJoin = node;
			return node;
			//return new JoinExpression(DbExpressionType.Join, node.Second, node.First, node.Result, node.Condition);
		}
	}

	/// <summary>
	/// evaluates JoinExpression and rewrites such nodes as ConstantExpression
	/// </summary>
	public class JoinExpressionRewriter : ExpressionVisitor
	{

		public override Expression Visit(Expression node) {
			switch ((DbExpressionType)node.NodeType) {
				case DbExpressionType.Join:
					return this.VisitJoin((JoinExpression)node);
			}
			return base.Visit(node);
		}

		private ConstantExpression VisitJoin(JoinExpression node) {
			return Expression.Constant(null);
		}
	}
}
